package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.ProcessDAO;
import com.dao.lx.RepairorderDAO;
import com.dao.lx.UserDAO;
import com.entity.Process;
import com.entity.Repairorder;
import com.entity.User;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class ProcessService {
	@Autowired
	private ProcessDAO processDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private RepairorderDAO repairorderDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param process
	 * @throws ServiceException
	 */
	public void addAndUpdateProcess(Process process) throws ServiceException{
		try {
			//判断用户是否存在
			if(process.getUserId()!=null){
				User user=userDAO.findOne(process.getUserId());
				if(user!=null){
					process.setUser(user);
				}else{
					process.setUser(null);
				}
			}
			//判断维修工单是否存在
			if(process.getRepairorderId()!=null){
				Repairorder repairorder=repairorderDAO.findOne(process.getRepairorderId());
				if(repairorder!=null){
					Process process3=processDAO.findByRepairorder(repairorder);
					if(process3!=null){
						process.setProcessId(process3.getProcessId());
					}
					process.setRepairorder(repairorder);
				}else{
					process.setRepairorder(null);
				}
			}
			Process process2=processDAO.findOne(process.getProcessId());
			if(process2==null){
				process.setProcessIfdelete(new BigDecimal(1));
			}
			process.setProcessIntodate(new Date());
			processDAO.save(process);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param processId
	 * @throws ServiceException
	 */
	public void deleteProcess(Long processId) throws ServiceException{
		try {
			processDAO.delete(processId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 单条查询
	 * @param processId
	 * @return
	 */
	public Process findOne(Long processId) {
		return processDAO.findOne(processId);
	}

}
